<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Publikasi extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->addBreadCrumb("Publikasi", site_url("publikasi"));
    }

    public function index() {
        redirect("publikasi/buletin");
        //$this->_renderPage("publikasi/home");
    }

    public function buletin($ed = "recent") {
        $this->_data["useDocViewer"] = true;

        $buletin = null;
        $buletins = pub_fetchall("buletin");

        $i = 0;
        foreach ($buletins as $bul) {
            if ($bul->name == $ed) {
                $buletin = $bul;
                break;
            } else if (($ed == "recent") && ($i == count($buletins) - 1)) {
                $buletin = $bul;
                redirect("publikasi/buletin/" . $buletin->name);
            }
            $i++;
        }

        if ($buletin != null) {
            $this->addBreadCrumb($buletin->title, site_url("publikasi/buletin/" . $buletin->name));
            $this->_data["pageTitle"] = $buletin->title;
            $this->_data["buletins"] = $buletins;
            $this->_data["buletin"] = $buletin;
            $this->_data["imageFB"] = "http://docs.google.com/viewer?url=" . $buletin->url . "&a=bi&pagenumber=1&w=200";
            $this->_renderPage("publikasi/buletin");
        } else {
            redirect("home/error");
        }
    }

    public function pb($ed = "recent") {
        $this->_data["useDocViewer"] = true;

        $pb = null;
        $pbs = pub_fetchall("pb");

        $i = 0;
        foreach ($pbs as $pub) {
            if ($pub->name == $ed) {
                $pb = $pub;
                break;
            } else if (($ed == "recent") && ($i == count($pbs) - 1)) {
                $pb = $pub;
                redirect("publikasi/pb/" . $pb->name);
            }
            $i++;
        }

        if ($pb != null) {
            $this->addBreadCrumb($pb->title, site_url("publikasi/pb/" . $pb->name));
            $this->_data["pageTitle"] = $pb->title;
            $this->_data["pbs"] = $pbs;
            $this->_data["pb"] = $pb;
            $this->_data["imageFB"] = "http://docs.google.com/viewer?url=" . $pb->url . "&a=bi&pagenumber=1&w=200";
            $this->_renderPage("publikasi/pb");
        } else {
            redirect("home/error");
        }
    }

    public function regulasi($ed = "recent") {
        $this->_data["useDocViewer"] = true;

        $regulasi = null;
        $regulasis = pub_fetchall("regulasi");

        $i = 0;
        foreach ($regulasis as $bul) {
            if ($bul->name == $ed) {
                $regulasi = $bul;
                break;
            } else if (($ed == "recent") && ($i == count($regulasis) - 1)) {
                $regulasi = $bul;
            }
            $i++;
        }

        if ($regulasi != null) {
            $this->_data["pageTitle"] = $regulasi->title;
            $this->_data["regulasis"] = $regulasis;
            $this->_data["regulasi"] = $regulasi;
            $this->_renderPage("publikasi/regulasi");
        } else {
            redirect("home/error");
        }
    }

    public function upload($type, $isPrev = false) {
        $result = new StdClass();
        $result->isSuccess = false;
        $validated = false;
        $fName = "";

        switch ($type) {
            case "buletin" :
                $authorized = Array("bulletin_master");
                if ($this->isAuth($authorized)) {
                    $validated = true;
                    $fName = $this->input->post("ed");
                }
                break;
            case "pb" :
                $authorized = Array("pb_master");
                if ($this->isAuth($authorized)) {
                    $validated = true;
                    $fName = $this->input->post("title");
                }
                break;
            case "regulasi" :
                $authorized = Array("regulasi_master");
                if ($this->isAuth($authorized)) {
                    $validated = true;
                    $fName = $this->input->post("title");
                }
                break;
        }

        if ($validated) {
            $pubConf = pub_conf($type);
            if ($pubConf != null) {
                $file = $_FILES["file"];
                $ext = strtolower(end(explode(".", $file["name"])));
                $fileConf = Array(
                    "allowed_types" => "pdf",
                    "max_size" => 0,
                    "upload_path" => APPPATH . "data/pub/" . $pubConf->pubDir . "/",
                    "file_name" => $pubConf->pubFile . $fName . ($isPrev ? ".prev." : ".") . $ext,
                    "overwrite" => true
                );

                $this->load->library("upload", $fileConf);
                if (!$this->upload->do_upload("file")) {
                    $result->message = $this->upload->display_errors();
                } else {
                    $result->isSuccess = true;
                }
            }
        }

        echo json_encode($result);
    }

}